package com.topscore.uke.ui.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.topscore.uke.R;
import com.topscore.uke.entities.CommoditySubData;
import com.topscore.uke.entities.PopularBrandBean;
import com.topscore.uke.entities.RecommendStuffBean;
import com.topscore.uke.ui.BaseSupportFragment;
import com.topscore.uke.ui.activities.StuffListActivity;
import com.topscore.uke.ui.widgets.DividerGridItemDecoration;
import com.topscore.uke.utils.DisplayImageOptionsManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class StuffListFragment extends BaseSupportFragment {

    @Bind(R.id.root_recycler)
    RecyclerView rootRecycler;
    private View rootView;

    private final int _Stuff_Recommend = 1001;
    private final int _Brand = 1002;

    private List<MultiItem> mMultiItemList;

    private MultiItemAdapter mMultiAdapter;

    private BaseQuickAdapter<CommoditySubData, BaseViewHolder> mRecommendStuffAdapter;
    private BaseQuickAdapter<PopularBrandBean, BaseViewHolder> mPopularBrandAdapter;

    private List<RecommendStuffBean> mStuffList;
    private List<PopularBrandBean> mBrandList;

    private OnBrandSelected mOnBrandSelected;

    public StuffListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_stuff_list, container, false);
        ButterKnife.bind(this, rootView);
        initViews();
        return rootView;
    }

    private void initViews() {
        initAdapter();
        rootRecycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        rootRecycler.setAdapter(mMultiAdapter);
        initData();
    }

    private void initAdapter() {
        initStuffAdapter();
        mMultiItemList = new ArrayList<>();
        mMultiItemList.add(new MultiItem(_Stuff_Recommend));
        mMultiItemList.add(new MultiItem(_Brand));
        mMultiAdapter = new MultiItemAdapter(mMultiItemList);
    }

    private void initData() {

        mBrandList = new ArrayList<>();
        mBrandList.add(new PopularBrandBean("KC", R.mipmap.logo_kiss_cat));
        mBrandList.add(new PopularBrandBean("ZSA", R.mipmap.logo_zsa_zsa_zsu));
        mBrandList.add(new PopularBrandBean("TS", R.mipmap.logo_tigrisso));
        mBrandList.add(new PopularBrandBean("KK", R.mipmap.logo_kiss_kitty));
        mBrandList.add(new PopularBrandBean("PT", R.mipmap.logo_patricia));
        mBrandList.add(new PopularBrandBean("KM", R.mipmap.logo_kasmase));
        mBrandList.add(new PopularBrandBean("MH", R.mipmap.logo_must_have));
        mPopularBrandAdapter.setNewData(mBrandList);
    }

    public void setUpStuffData(List<CommoditySubData> dataList) {
        mRecommendStuffAdapter.setNewData(dataList);
    }

    private void initStuffAdapter() {
        mRecommendStuffAdapter = new BaseQuickAdapter<CommoditySubData, BaseViewHolder>(R.layout.stuff_grid_item, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder holder, CommoditySubData item) {
//                if (item.isMore()){
//                    holder.getView(R.id.see_more).setVisibility(View.VISIBLE);
//                    holder.getView(R.id.stuff_layout).setVisibility(View.GONE);
//                }else {
//                    holder.getView(R.id.see_more).setVisibility(View.GONE);
//                    holder.getView(R.id.stuff_layout).setVisibility(View.VISIBLE);
//                    ImageLoader.getInstance().displayImage(item.getImage(), (ImageView) holder.getView(R.id.stuff_img_iv));
//                    holder.setText(R.id.item_title,item.getTitle());
//                }
                holder.getView(R.id.see_more).setVisibility(View.GONE);
                holder.getView(R.id.stuff_layout).setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(item.getImgPath(), (ImageView) holder.getView(R.id.stuff_img_iv), DisplayImageOptionsManager.getInstance().getDefaultOptions());
                holder.setText(R.id.item_title, item.getSubClassName());

                holder.getConvertView().setOnClickListener(view -> {
                    Intent intent = new Intent(mContext, StuffListActivity.class);
                    intent.putExtra("classId", item.getSubClassId());
                    startActivity(intent);
                });
            }
        };
        mPopularBrandAdapter = new BaseQuickAdapter<PopularBrandBean, BaseViewHolder>(R.layout.popular_brand_item, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder holder, PopularBrandBean item) {
//                ImageLoader.getInstance().displayImage(item.getImage(), (ImageView) holder.getView(R.id.brand_img_iv));
                ((ImageView) holder.getView(R.id.brand_img_iv)).setImageResource(item.getImageRes());
                holder.itemView.setOnClickListener(view -> {
                    if (mOnBrandSelected != null) {
                        mOnBrandSelected.onSelecteBrand(item.getBrand());
                    }
                });
            }
        };
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void setOnBrandSelected(OnBrandSelected onBrandSelected) {
        this.mOnBrandSelected = onBrandSelected;
    }

    private class MultiItem implements MultiItemEntity {

        private int itemType;

        public MultiItem(int itemType) {
            this.itemType = itemType;
        }

        public void setItemType(int itemType) {
            this.itemType = itemType;
        }

        @Override
        public int getItemType() {
            return itemType;
        }
    }

    private class MultiItemAdapter extends BaseMultiItemQuickAdapter<MultiItem, BaseViewHolder> {

        /**
         * Same as QuickAdapter#QuickAdapter(Context,int) but with
         * some initialization data.
         *
         * @param data A new list is created out of this one to avoid mutable list
         */
        public MultiItemAdapter(List<MultiItem> data) {
            super(data);
            addItemType(_Stuff_Recommend, R.layout.commodity_recommend_layout);
            addItemType(_Brand, R.layout.popular_brand_layout);
        }

        @Override
        protected void convert(BaseViewHolder holder, MultiItem item) {
            switch (item.getItemType()) {
                case _Stuff_Recommend:
                    RecyclerView stuffRecycler = holder.getView(R.id.stuff_recycler);
                    stuffRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
                    stuffRecycler.addItemDecoration(new DividerGridItemDecoration(getActivity(), R.drawable.stuff_grid_decoration));
                    stuffRecycler.setAdapter(mRecommendStuffAdapter);
                    break;

                case _Brand:
                    RecyclerView brandRecycler = holder.getView(R.id.brand_recycler);
                    brandRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
                    brandRecycler.addItemDecoration(new DividerGridItemDecoration(getActivity(), R.drawable.stuff_grid_decoration));
                    brandRecycler.setAdapter(mPopularBrandAdapter);
                    break;
            }
        }
    }

    public interface OnBrandSelected {
        void onSelecteBrand(String brand);
    }
}
